package top.kaoshanji.r2ch07b;

/**
 * 代码清单-r2ch07b-223页a：性能监控逻辑
 * @author kaoshanji
 * ^_^
 * create time 2025/5/7 11:22
 */
public class PerformanceMonitor {

    private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<>();

    public static void begin(String method) {
        System.out.println("begin monitor...");

        MethodPerformance mp = performanceRecord.get();
        if (mp == null) {
            mp = new MethodPerformance(method);
            performanceRecord.set(mp);
        } else {
            mp.reset(method);
        }
    }

    public static void end() {
        System.out.println("end monitor...");

        MethodPerformance mp = performanceRecord.get();
        mp.printPerformance();
    }

}
